﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace WAMM.WebUI.Models {
    public class PersonWebUIModel {

        private IList<GenderChoice> _genders = new List<GenderChoice>() 
                    {new GenderChoice(null, "-select-"),
                     new GenderChoice("M", "Male"), 
                     new GenderChoice("F", "Female")};

        [HiddenInput(DisplayValue = false)]
        public int? Id { get; set; }
        public string PictureUrl { get; set; }
        public string ChangePictureUrl { get; set; }
        [Display(Name = "First Name")]
        [Required(ErrorMessage = Wamm.Model.Validation.ValidatePerson.FIRST_NAME_IS_REQUIRED)]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        [Required(ErrorMessage = Wamm.Model.Validation.ValidatePerson.LAST_NAME_IS_REQUIRED)]
        public string LastName { get; set; }
        public string Gender { get; set; }
        public SelectList GenderChoices {
            get { return new SelectList(Genders, "Id", "Name", SelectedGender); }
        }
        public bool Guest { get; set; }
        [Display(Name = "Birth Date")]
        [DisplayFormat(ApplyFormatInEditMode = true,
                       ConvertEmptyStringToNull = true,
                       DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime? BirthDate { get; set; }
        public byte[] Picture { get; set; }
        [Display(Name= "Primary Contact For Family")]
        public bool PrimaryContactForFamily { get; set; }
        public FamilyWebUIModel Family { get; set; }
        public bool Deleted { get; set; }
        [Display(Name = "Added by")]
        public string AddedBy { get; set; }
        [Display(Name = "Added at")]
        public DateTime? AddedTstamp { get; set; }
        [Display(Name = "Last updated by")]
        public string LastUpdatedBy { get; set; }
        [Display(Name = "Last updated at")]
        public DateTime? LastUpdatedTstamp { get; set; }

        public PersonWebUIModel() {
            PictureUrl = "/content/NoPicture.gif";
            ChangePictureUrl = "jQuery('#dialog').dialog('open'); return false";
        }
        private IList<GenderChoice> Genders {
            get {
                return new List<GenderChoice>() 
                    {new GenderChoice(null, "-select-"),
                     new GenderChoice("M", "Male"), 
                     new GenderChoice("F", "Female")};                
            }
        }

        private GenderChoice SelectedGender {
            get {
                foreach (var genderChoice in _genders) {
                    if (Gender == genderChoice.Id) {
                        return genderChoice;
                    }
                }
                return _genders[0];
            }
        }
        public class GenderChoice {
            public string Id { get; set; }
            public string Name { get; set; }

            public GenderChoice(string id, string name) {
                Id = id;
                Name = name;
            }
        }
    }
}